import { BASE_URL, get, post,put } from './request'
import Promisify from '../utils/promisify'

module.exports = {
  loginWechat:function(code,token="",isTest=false,type=""){
    return get(`/wechat/login?code=${code}&token=${token}&isTest=${isTest}&type=${type}`);
  },
  loginTest:function(){
    return get(`/wechat/login/test`)
  },
  sendCode: function (phone) {
    return get(`/wechat/send/${phone}?type=wechat`)
  },
  checkCode: function (phone, code) {
    return get('/wechat/check', { phone, code, type: 'wechat' })
  },
  uploadImg: function (filepath) {
    wx.uploadFile({
      url: 'https://localhost/common/sysFile/upload/img',
      filePath: filepath,
      name: 'picture',
      success:(res)=>{
        console.log('res',res);
      }
    });
    return Promisify(wx.uploadFile)({
      // url: `${BASE_URL}/common/sysFile/upload/img`,
      url:'https://localhost/common/sysFile/upload/img',
      filePath: filepath,
      name: 'picture'
    }).then(res => {
      if (res.statusCode != 200) {
        try {
          res.data = JSON.parse(res.data)
        } catch (e) {

        }
        wx.showModal({
          title: '上传图片失败',
          content: (res.data && res.data.message) ? res.data.message : '发生未知错误',
          showCancel: false
        })
        return Promise.reject(new Error('上传图片失败'))
      } else {
        return res.data
      }
    })
  },
  updateUserInfoById: function (id, params) {
    return put(`/wechat/user/${id}`, params)
  },
  getUserInfoById: function (id) {
    return get(`/wechat/user/${id}`)
  }
}
